//
//  ShopPayForViewController.m
//  HuiMWorld
//
//  Created by chirui on 2018/1/17.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "ShopPayForViewController.h"
#import "UserTicketsViewController.h"
//#import "SellUserTicketsViewController.h"  //外卖订单

@interface ShopPayForViewController (){
    
    NSString * pay_pwdStr;
    NSString * _payType;
}
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *ticketsMessage;
@property (weak, nonatomic) IBOutlet UIButton *wxBtn;
@property (weak, nonatomic) IBOutlet UIButton *zfbBtn;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
@property (weak, nonatomic) IBOutlet UILabel *emsLabel;


@end

@implementation ShopPayForViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    TitleView(@"支付方式");
    _payType = @"wxpay";
    _wxBtn.selected = YES;
    
//    //微信
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(payWxResultOpt:)
//                                                 name: WX_NOTIFY_KEY
//                                               object:nil];
//
//    //支付宝
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(ZFBSuccess:)
//                                                 name: @"zfb"
//                                               object:nil];
//
    self.priceLabel.text = [NSString stringWithFormat:@"￥%@",self.dataDic[@"order_price"]];
     self.emsLabel.text = [NSString stringWithFormat:@"运费：￥%@",self.dataDic[@"freight"]];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_return_back" target:self action:@selector(back)];
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)chooseTicketsClick:(UIButton *)sender {
    
    UserTicketsViewController * ticket  = [[UserTicketsViewController alloc]init];
 
    ticket.priceStr = self.dataDic[@"order_price"];
   
    ticket.orderType = @"shop";
    ticket.block = ^(NSDictionary * dic) {

        _ticketsMessage.text = [NSString stringWithFormat:@"-￥%@",dic[@"price"]];
    };
    [self.navigationController pushViewController:ticket animated:YES];
}

- (IBAction)wxBtnClick:(id)sender {
      _payType = @"wxpay";
      _wxBtn.selected = YES;
      _zfbBtn.selected = NO;
    
}

- (IBAction)zfuBtnClick:(id)sender {
    _payType = @"alipay";
    _wxBtn.selected = NO;
    _zfbBtn.selected = YES;
  
    
}

- (IBAction)sureBtnClick:(id)sender {
    
   
    if ([_payType isEqualToString:@"alipay"]||[_payType isEqualToString:@"wxpay"]) {
        
        [self topUpBanceRequest];
    }
    
    
}

#pragma mark - 支付密码输入完成回调
- (void)inputFinishedWithText:(NSString *)text {
    // text为用户输入的6位密码
    
    
    
    self.verifyCodeInputView.hidden = YES;
    pay_pwdStr = text;
    for(UITextField *textField in self.verifyCodeInputView.inputView.textFieldArray) {
        textField.text = @"";
    }
    
    if (pay_pwdStr.length!=6) {
        [SHJudgeTools MBProgressHUDPromptWith:@"支付密码格式不正确" On:self.view];
        return;
    }else {
    
        
    }
    
}



-(void)topUpBanceRequest {
    
//
//    NSDictionary * dic = @{@"user_id":USER_DEFAULTGet(@"user_id"),
//                           @"token":USER_DEFAULTGet(@"token"),
//                           @"order_id":self.dataDic[@"order_id"],
//                           @"pay_type":_payType
//                           };
//    SHuiMBhubNoTimeLoding;
//    [PostNetWorking postNetWorkingStr:RequestUrlStr(payFor_order_api) andparameters:dic Success:^(id responseObject) {
//        SHuiMBHidleYes;
//
//        if ([responseObject[@"code"] intValue]==200) {
//
//            if ([_payType isEqualToString:@"wxpay"]) {
//
//                PayReq* req             = [[PayReq alloc] init];
//                req.openID              = [responseObject[@"datas"][@"Appparam"] objectForKey:@"appid"];
//                req.partnerId           = [responseObject[@"datas"][@"Appparam"] objectForKey:@"partnerid"];
//                req.prepayId            = [responseObject[@"datas"][@"Appparam"] objectForKey:@"prepayid"];
//                req.nonceStr            = [responseObject[@"datas"][@"Appparam"] objectForKey:@"noncestr"];
//                req.timeStamp           = [[responseObject[@"datas"][@"Appparam"] objectForKey:@"timestamp"] intValue];
//                req.package             = [responseObject[@"datas"][@"Appparam"] objectForKey:@"packages"];
//                req.sign                = [responseObject[@"datas"][@"Appparam"] objectForKey:@"sign"];
//
//                [WXApi sendReq:req];
                
//            }else {
    
//                NSString *appScheme = @"HuiMWorld";
//
//                [[AlipaySDK defaultService] payOrder:responseObject[@"datas"][@"pay_string"]  fromScheme:appScheme callback:^(NSDictionary *resultDic) {
//                    NSLog(@"reslut = %@",resultDic);
//
//                }];
//            }
//        } else {
//
//            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
//        }
//
//    } failed:^(NSError *error) {
//        SHuiMBHidleYes;
//        SHuiMBhudTextNetError;
//    }];

}


//微信支付完成通知
-(void)payWxResultOpt:(NSNotification *)notification
{
//    PayResp *response=(PayResp*)notification.object;
//
//    if (response.errCode == WXSuccess)
//    {
//
//        FinishPayForViewController * finish = [[FinishPayForViewController alloc]init];
//        finish.priceStr =  self.dataDic[@"order_price"];
//        [self.navigationController pushViewController:finish animated:YES];
//
//    }
//    else
//    {
//        UIAlertView *alerte = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"支付失败了~~！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
//        [alerte show];
//    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (alertView.tag == 10001) {
        if (buttonIndex == 0) {
            
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }
}
-(void)ZFBSuccess:(NSNotification *)notification {
    
  
}

-(void)dealloc{
    
    //第一种方法.这里可以移除该控制器下的所有通知
    
    NSLog(@"移除了所有的通知");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}


@end
